package com.java_project.src;
import java.io.*;
import java.util.*;


public class ProductService {
    private final Map<String, Product> productData = new HashMap<>();
    //从配置文件读取路径
    private String dataFilePath;
    public ProductService() {
        loadConfig();
        loadData();
        if(productData.isEmpty()) {
            Set<String> tags1 = new HashSet<>();
            tags1.add("编程");
            tags1.add("畅销");
            productData.put("p001", new Product("p001", "Java从入门到精通", 99.0, 100, tags1));
            productData.put("p007", new Product("p007", "C语言入门教程", 45.0, 200, tags1));
            Set<String> tags2 = new HashSet<>();
            tags2.add("数据库");
            tags2.add("技术");
            productData.put("p002", new Product("p002", "高性能MySQL", 128.5, 50, tags2));
            productData.put("p003", new Product("p003", "颈椎康复指南", 45.0, 200, null));
            productData.put("p004", new Product("p004", "生发秘籍", 88.0, 0, null));
            productData.put("p005", new Product("p005", "云计算导论", 149.0, 80, null));
            productData.put("p006", new Product("p006", "大数据技术原理与应用", 89.5, 120, null));
        }
    }
    private void loadConfig(){
        Properties props = new Properties();
        try(InputStreamReader isr=new InputStreamReader(
                new FileInputStream("config.properties"),"UTF-8")){
            props.load(isr);
            dataFilePath = props.getProperty("data.file.path","products.dat");
        }catch(FileNotFoundException e){
            dataFilePath="products.dat";
            System.out.println("配置文件未找到，使用默认路径：" + dataFilePath);
        }catch (IOException e){
            e.printStackTrace();
            dataFilePath="products.dat";
        }
    }
    //保存数据到文件
    public void saveData(){
        try(ObjectOutputStream oos=new ObjectOutputStream(
                new FileOutputStream(dataFilePath))){
            oos.writeObject(productData);
            System.out.println("数据已成功保存到文件：" + dataFilePath);
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("保存数据到文件时出错：" +e.getMessage());
        }
    }
    //从文件加载数据
    @SuppressWarnings("unchecked")
    private void loadData(){
        File file=new File(dataFilePath);
        if(!file.exists()){
            System.out.println("数据文件路径不存在，将使用初始测试数据" );
            return;
        }
        try(ObjectInputStream ois=new ObjectInputStream(
                new FileInputStream(dataFilePath))){
            productData.clear();
            productData.putAll((Map<String, Product>)ois.readObject());
            System.out.println("数据已成功加载到内存：" + dataFilePath);
        }catch(FileNotFoundException e){
            System.out.println("数据文件未找到，将使用初始测试数据");
        }catch(IOException | ClassNotFoundException e){
            e.printStackTrace();
            System.out.println("加载数据失败，将使用初始测试数据");
        }
    }
    public List<Product> getAllProducts() {
        return new ArrayList<>(productData.values());
    }
    public Product findProductById(String id) {
        return productData.get(id);
    }
}